<template>
	<view>
		<view class="margin padding" style="margin-top:100rpx;">
			<u-message-input :maxlength="4" mode="bottomLine" :focus="true" :breathe="true" @finish="finish"></u-message-input>
		</view>
	</view>
</template>

<script>
import qs from 'qs';
export default {
	data() {
		return {
			info: {
				username: '', // 手机号
				password: '', // 验证码
				grant_type: 'password', // 授权类型
				scope: 'request',
				loginType: 'mobile' // 登录类型
			}
		};
	},
	onLoad: function(option) {
		this.info.username = option.username || '';
	},
	computed: {},
	methods: {
		finish: function(code) {
			this.info.password = code;
			this.$request.post(
				`/oauth/token?${qs.stringify(this.info)}`,
				{},
				res => {
					if (res.access_token) {
						uni.setStorageSync(this.$config.tokenName, res.access_token);
						this.$request.get(
							'/admin/sys/user_info',
							{},
							res => {
								uni.setStorageSync(this.$config.userInfoName, res.data);
								uni.switchTab({
									url: '../index/index',
									fail: () => {
										uni.redirectTo({
											url: '../index/index',
										})
									}
								});
							},
							true,
							true,
							true
						);
					}
				},
				true,
				true,
				false
			);
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #ffffff;
}
</style>
